<?php
/**
*
* @package MoltX QuickChat phpBB Administration Control Panel
* @author $Author: moltendorf $
* @version $Id: acp_molten_chat.php 190 2009-10-05 06:15:53Z moltendorf $
* @latest $URL: http://molten-chat.googlecode.com/svn/trunk/source/includes/acp/acp_molten_chat.php $
* @copyright (c) 2005-2009 Matthew Oltendorf
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package acp
*/
class acp_molten_chat
{
	var $u_action;
	var $new_config;
	
	function main($id, $mode)
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

		$installed = isset($config['molten_chat_installed']);
		
		if ($installed)
		{
			switch ($mode)
			{
				default:
					$user->add_lang('acp/board');

					$action	= request_var('action', '');
					$submit = (isset($_POST['submit'])) ? true : false;

					$form_key = 'acp_board';
					add_form_key($form_key);

					switch ($mode)
					{
						case 'update':
							$user->add_lang('install');

							$this->tpl_name = 'acp_molten_chat_update';
							$this->page_title = 'MOLTEN_CHAT_UPDATE_TITLE';

							$file = 'molten_chat_version.txt';
							$match = array();
							$regex = '/\\$Version:[ ]+([0-9]+(?:[-.](?:a|b|dev|RC)?[0-9]+)*)[ ]+\\$/';

							if (file_exists($phpbb_root_path . 'includes/molten_chat/' . $file))
							{
								$local = file_get_contents($phpbb_root_path . 'includes/molten_chat/' . $file);
							}
							else
							{
								trigger_error($user->lang['MOLTEN_CHAT_UPDATE_ERROR_LOCAL'], E_USER_WARNING);
							}

							preg_match($regex, $local, $match);
							$current_version = $match[1];

							preg_match('/\\$Update:[ ]+(.*?)[ ]+\\$/', $local, $match);
							$update_link = append_sid($phpbb_root_path . $match[1], false, true);

							$error = $null = '';
							$remote = get_remote_file('molten-chat.googlecode.com', '/svn/trunk/source/includes/molten_chat', $file, $error, $null);
							unset($null);

							preg_match($regex, $remote, $match);
							$latest_version = $match[1];

							preg_match('/\\$Announcement:[ ]+(.*?)[ ]+\\$/', $remote, $match);
							$announcement_url = $match[1];
							
							if ($error == $user->lang['FILE_NOT_FOUND'] . ': ' . $file)
							{
								trigger_error($user->lang['MOLTEN_CHAT_UPDATE_ERROR_REMOTE'], E_USER_WARNING);
							}

							$up_to_date_automatic = (version_compare(str_replace('rc', 'RC', strtolower($config['molten_chat_version'])), str_replace('rc', 'RC', strtolower($current_version)), '<')) ? false : true;
							$up_to_date = (version_compare(str_replace('rc', 'RC', strtolower($current_version)), str_replace('rc', 'RC', strtolower($latest_version)), '<')) ? false : true;

							$template->assign_vars(array(
								'S_UP_TO_DATE'		=> $up_to_date,
								'S_UP_TO_DATE_AUTO'	=> $up_to_date_automatic,
								'U_ACTION'			=> $this->u_action,

								'LATEST_VERSION'	=> $latest_version,
								'CURRENT_VERSION'	=> $current_version,
								'AUTO_VERSION'		=> $config['molten_chat_version'],

								'UPDATE_INSTRUCTIONS'	=> sprintf($user->lang['MOLTEN_CHAT_UPDATE_INSTRUCTIONS_' . (!$up_to_date ? 'START' : 'INCOMPLETE')], $announcement_url, $update_link),
							));

							return;
						break;

						case 'general':
							$display_vars = array(
								'title'	=> 'MOLTEN_CHAT_SETTINGS_GENERAL',
								'vars'	=> array(
									'legend1'								=> 'MOLTEN_CHAT_SETTINGS_GENERAL_ENABLE_LEGEND',
									'molten_chat_enable'					=> array('lang' => 'MOLTEN_CHAT_SETTINGS_GENERAL_ENABLE_SWITCH',		'validate' => 'bool',			'type' => 'radio:yes_no',																																																															'explain' => true),
									'legend2'								=> 'MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_LEGEND',
									'molten_chat_cache_type'				=> array('lang' => 'MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_CACHE',			'validate' => 'int:0:2',		'type' => 'select',			'function' => 'build_select',	'params' => array(array('MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_CACHE_APC', 'MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_CACHE_SHMOP', 'MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_CACHE_XCACHE'), $config['molten_chat_cache_type']),	'explain' => true),
									'molten_chat_storage_type'				=> array('lang' => 'MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_STORAGE',		'validate' => 'int:0:0',		'type' => 'select',			'function' => 'build_select',	'params' => array(array('MOLTEN_CHAT_SETTINGS_GENERAL_MODULE_STORAGE_FILESYSTEM'), $config['molten_chat_cache_type']),																									'explain' => true),
									'legend3'								=> 'MOLTEN_CHAT_SETTINGS_GENERAL_MESSAGE_LEGEND',
									'molten_chat_messages_reply'			=> array('lang' => 'MOLTEN_CHAT_SETTINGS_GENERAL_MESSAGE_REPLY',		'validate' => 'bool',			'type' => 'radio:yes_no',																																																															'explain' => true),
									'molten_chat_messages_topic'			=> array('lang' => 'MOLTEN_CHAT_SETTINGS_GENERAL_MESSAGE_TOPIC',		'validate' => 'bool',			'type' => 'radio:yes_no',																																																															'explain' => true),
								)
							);
						break;

						case 'cache':
							if (!$config['molten_chat_enable'])
							{
								trigger_error('MOLTEN_CHAT_SETTINGS_CACHE_ERROR_DISABLED', E_USER_WARNING);
							}

							if (!defined('molten_chat_enabled'))
							{
								trigger_error('MOLTEN_CHAT_SETTINGS_CACHE_ERROR_MODULE', E_USER_WARNING);
							}

							$display_vars = array(
								'title'	=> 'MOLTEN_CHAT_SETTINGS_CACHE',
								'vars'	=> array(
									'legend1'								=> 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_LEGEND',
									'molten_chat_cache_initial_retrieve'	=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_INITIAL_RETRIEVE',	'validate' => 'int:2:500',		'type' => 'text:5:3',																																																																'explain' => true),
									'molten_chat_cache_message_length'		=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_MESSAGE_LENGTH',	'validate' => 'int:32:4096',	'type' => 'text:5:4',																																																																'explain' => true),
									'molten_chat_cache_level_messages'		=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_LEVEL_MESSAGES',	'validate' => 'int:16:4096',	'type' => 'text:5:4',																																																																'explain' => true),
									'molten_chat_cache_level_count'			=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_LEVEL_COUNT',		'validate' => 'int:1:100',		'type' => 'text:5:3',																																																																'explain' => true),
									'molten_chat_cache_level_age'			=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_LEVEL_AGE',		'validate' => 'int:1:1440',		'type' => 'text:5:4',																																																																'explain' => true),
									'molten_chat_cache_update'				=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_UPDATE',			'validate' => 'int:5:300',		'type' => 'text:5:3',																																																																'explain' => true),
									'molten_chat_cache_connections'			=> array('lang' => 'MOLTEN_CHAT_SETTINGS_CACHE_BASIC_CONNECTIONS',		'validate' => 'int:1:10',		'type' => 'text:5:2',																																																																'explain' => true),
								)
							);
						break;

						case 'debug':
							$this->tpl_name = 'acp_molten_chat_debug';
							$this->page_title = 'MOLTEN_CHAT_DEBUG_TITLE';

							$template->assign_vars(array(
								'S_MODULE_CACHE_APC' => extension_loaded('apc'),
								'S_MODULE_CACHE_SHMOP' => extension_loaded('shmop'),
								'S_MODULE_CACHE_XCACHE' => extension_loaded('xcache'),
							));
							return;
						break;

						default:
							trigger_error('NO_MODE', E_USER_WARNING);
						break;
					}

					if (isset($display_vars['lang']))
					{
						$user->add_lang($display_vars['lang']);
					}

					$this->new_config = $config;
					$cfg_array = (isset($_REQUEST['config'])) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
					$error = array();

					// We validate the complete config if wished.
					validate_config_vars($display_vars['vars'], $cfg_array, $error);

					if ($submit && !check_form_key($form_key))
					{
						$error[] = $user->lang['FORM_INVALID'];
					}
					// Do not write values if there is an error.
					if (sizeof($error))
					{
						$submit = false;
					}
					// Otherwise, update room as well.
					elseif ($submit && $mode == 'cache')
					{
						if (defined('molten_chat_enabled'))
						{
							molten_chat_server::modify('system_index', true, false, $cfg_array['molten_chat_cache_level_count'], $cfg_array['molten_chat_cache_level_messages'], $cfg_array['molten_chat_cache_initial_retrieve'], $cfg_array['molten_chat_cache_level_age'], false);
						}
					}

					// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
					foreach ($display_vars['vars'] as $config_name => $null)
					{
						if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
						{
							continue;
						}

						$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];

						if ($submit)
						{
							set_config($config_name, $config_value);
						}
					}

					if ($submit)
					{
						add_log('admin', 'LOG_CONFIG_MOLTEN_CHAT_' . strtoupper($mode));

						trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
					}

					$this->tpl_name = 'acp_board';
					$this->page_title = $display_vars['title'] . '_TITLE';

					$template->assign_vars(array(
						'L_TITLE'			=> $user->lang[$display_vars['title']],
						'L_TITLE_EXPLAIN'	=> $user->lang[$display_vars['title'] . '_EXPLAIN'],

						'S_ERROR'			=> (sizeof($error)) ? true : false,
						'ERROR_MSG'			=> implode('<br />', $error),

						'U_ACTION'			=> $this->u_action)
					);

					// Output relevant page.
					foreach ($display_vars['vars'] as $config_key => $vars)
					{
						if (!is_array($vars) && strpos($config_key, 'legend') === false)
						{
							continue;
						}

						if (strpos($config_key, 'legend') !== false)
						{
							$template->assign_block_vars('options', array(
								'S_LEGEND'		=> true,
								'LEGEND'		=> (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
							);

							continue;
						}

						$type = explode(':', $vars['type']);

						$l_explain = '';
						if ($vars['explain'] && isset($vars['lang_explain']))
						{
							$l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
						}
						else if ($vars['explain'])
						{
							$l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
						}

						$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);

						if (empty($content))
						{
							continue;
						}

						$template->assign_block_vars('options', array(
							'KEY'			=> $config_key,
							'TITLE'			=> (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
							'S_EXPLAIN'		=> $vars['explain'],
							'TITLE_EXPLAIN'	=> $l_explain,
							'CONTENT'		=> $content,
							)
						);

						unset($display_vars['vars'][$config_key]);
					}
				break;
			}

			return;
		}

		// Not installed.
		trigger_error($user->lang['MOLTEN_CHAT_ERROR_NOT_INSTALLED'] . adm_back_link($this->u_action), E_USER_WARNING);
		//'MoltX QuickChat 2.1 for phpBB 3 has not been properly installed. Please refer to the MODX file, and complete the installation.'
	}
}